find();
window.addEventListener("focus", find);

function find(){
	var port = chrome.extension.connect();
	var size = 0;
	var actsize = 0;
  	var images = [];
	port.onMessage.addListener(function(data){
		var result = document.getElementsByTagName("img");
		for (var key in result) {
			if (!isNaN(result[key].width)) {
				actsize = result[key].width * result[key].height;
				if (actsize > size) {
					images.shift();
					images.push(result[key].src);
					size = (result[key].width * result[key].height);
				}	else	{
					console.log("size too small");
				}
			}			
		}
		if (data == "bg") {
			document.location.href=images[0];
		}	else {
			port.postMessage(images);
		}
	});
}